package com.sskj.mine.asset;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.TipUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.asset.data.AddresAllBean;
import com.sskj.mine.asset.data.AddressBean;
import com.sskj.mine.asset.data.AddressListBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/05/04
 */
public class AddressListActivity extends BaseActivity<AddressListPresenter> {

    private static final String KEY_TYPE = "type";//1提币页面跳转过来，2.我的页面跳转过来

    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;

    private SlimAdapter slimAdapter;

    private int mType;


    @Override
    public int getLayoutId() {
        return R.layout.mine_activity_address_list;
    }

    @Override
    public AddressListPresenter getPresenter() {
        return new AddressListPresenter();
    }

    public static void start(Context context, int type) {
        Intent intent = new Intent(context, AddressListActivity.class);
        intent.putExtra(KEY_TYPE, type);
        context.startActivity(intent);
    }

    @Override
    public void initData() {
        mPresenter.addressList();
    }

    @Override
    public void initView() {
        mType = getIntent().getIntExtra(KEY_TYPE, -1);
        setTitle(App.INSTANCE.getString(R.string.mineaddAddressActivity1));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapter = SlimAdapter.create().register(R.layout.mine_recy_item_address_list, new SlimInjector<AddressListBean>() {
            @Override
            public void onInject(AddressListBean data, IViewInjector injector, List list) {
                injector
                        .text(R.id.tvTitle, data.getCode())
                        .clicked(R.id.ivAdd, v -> {
                            ARouter.getInstance().build(RConfig.MINE_ADD_ADDRESS).withString(Constans.TYPE, data.getType()).navigation();
                        })
                        .with(R.id.recyclerViewItem, view -> {
                            RecyclerView recyclerViewItem = (RecyclerView) view;
                            recyclerViewItem.setLayoutManager(new LinearLayoutManager(AddressListActivity.this));

                            SlimAdapter slimAdapterItem = SlimAdapter.create().register(R.layout.mine_item_address, new SlimInjector<AddressBean>() {
                                @Override
                                public void onInject(AddressBean data, IViewInjector injector, List<Object> list) {
                                    injector
                                            .text(R.id.name, data.getRemark())
                                            .text(R.id.address_url, data.getToAddr())
                                            .clicked(R.id.mineTextview6, v -> {
                                                TipUtil.getSureCancelTip(AddressListActivity.this, App.INSTANCE.getString(R.string.mineaddressListActivity2), () -> {
                                                    mPresenter.delete(data.getId() + "");
                                                });
                                            });
                                }

                            }).attachTo(recyclerViewItem).updateData(data.getList());
                            ItemClickSupport.addTo(recyclerViewItem)
                                    .setOnItemClickListener((recyclerView, i, view1) -> {
                                        if (mType == 1) {
                                            RxBus.getDefault().post(slimAdapterItem.getDataItem(i));
                                            finish();
                                        }

                                    });
                        });

            }
        }).attachTo(recyclerView).updateData(new ArrayList());


    }


    public static void start(Context context) {
        Intent intent = new Intent(context, AddressListActivity.class);
        context.startActivity(intent);
    }

    public void updateUI(AddresAllBean data) {

        ArrayList<AddressListBean> addressListBeans = new ArrayList<>();
        AddressListBean eth = new AddressListBean("1", "ETH");

        eth.setList(data.getETH());

        addressListBeans.add(eth);
        AddressListBean btc = new AddressListBean("2", "BTC");

        btc.setList(data.getBTC());

        addressListBeans.add(btc);
        slimAdapter.updateData(addressListBeans);
//        new AddressListBean("3", "BCH");
    }

    @Override
    protected void onResume() {
        if (mPresenter != null) {
            mPresenter.addressList();
        }
        super.onResume();
    }

    public void deleteSuccess() {
        mPresenter.addressList();
    }
}
